<template>
  <!--诉讼流程管理界面-->
  <d2-container>
    <el-form :inline="true" size="mini" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="queryItems[0].value" :placeholder="$t('arbitrationFlow.name')" clearable/>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">{{ $t('query') }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="$hasPermission('sys:arbitrationFlow:save')" type="primary" @click="addOrUpdateHandle()">{{ $t('add') }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="$hasPermission('sys:arbitrationFlow:delete')" type="danger" @click="deleteHandle()">{{ $t('deleteBatch') }}</el-button>
      </el-form-item>
    </el-form>
    <el-table
      size="mini"
      v-loading="dataListLoading"
      :data="dataList"
      border
      @selection-change="dataListSelectionChangeHandle"
      @sort-change="dataListSortChangeHandle"
      style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50"/>
      <el-table-column prop="name" :label="$t('arbitrationFlow.name')" header-align="center" align="center">
        <template slot-scope="scope">
          <span v-if="dataForm.parentId !== '0'">{{ scope.row.name }}</span>
          <el-button v-else type="text" @click="childHandle(scope.row)">{{ scope.row.name }}</el-button>
        </template>
      </el-table-column>
      <el-table-column v-if="dataForm.parentId !== '0'" prop="days" :label="$t('arbitrationFlow.days')" header-align="center" align="center"/>
      <el-table-column prop="sort" :label="$t('sort')" sortable="custom" header-align="center" align="center"/>
      <el-table-column prop="status" :label="$t('status')" sortable="custom" header-align="center" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" size="mini" type="danger">{{ $t('status0') }}</el-tag>
          <el-tag v-else-if="scope.row.status === 1" size="mini" type="success">{{ $t('status1') }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="remark" :label="$t('remark')" header-align="center" align="center"/>
      <el-table-column prop="createTime" :label="$t('createTime')" sortable="custom" header-align="center" align="center" width="180"/>
      <el-table-column  :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
        <template slot-scope="scope">
          <el-button v-if="$hasPermission('sys:arbitrationFlow:status')"  type="text" size="mini" @click="enableHandle(scope.row)">{{ scope.row.status === 0?$t('status1'):$t('status0')}}</el-button>
          <el-button v-if="$hasPermission('sys:arbitrationFlow:update')" type="text" size="mini" @click="addOrUpdateHandle(scope.row)">{{ $t('update') }}</el-button>
          <el-button v-if="$hasPermission('sys:arbitrationFlow:delete')" type="text" size="mini" @click="deleteHandle(scope.row.id)">{{ $t('delete') }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"/>
    <!-- 分页 -->
    <el-pagination
      slot="footer"
      :current-page="page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="limit"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="pageSizeChangeHandle"
      @current-change="pageCurrentChangeHandle">
    </el-pagination>
  </d2-container>
</template>

<script>
import { mapMutations } from 'vuex'
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './arbitrationFlow-add-or-update'
import { moduleRoutes } from '@/router'
export default {
  mixins: [ mixinViewModule ],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/arbitrationFlow/page',
        getDataListIsPage: true,
        deleteURL: '/arbitrationFlow/delete',
        deleteIsBatch: true,
        enableURL: '/arbitrationFlow/enable'
      },
      dataForm: {
        parentId: '0',
        name: '',
        type: '',
        value: ''
      },
      queryItems: [
        {
          field: 'name',
          as: 'EQ',
          value: ''
        },
        {
          field: 'parentId',
          as: 'EQ',
          value: '0'
        }
      ],
      order: 'ASC',
      orderField: 'sort'
    }
  },
  components: {
    AddOrUpdate
  },
  watch: {
    $route ($route) {
      // 通过路由参数pid, 控制列表请求操作
      this.dataForm.parentId = $route.params.parentId || '0'
      this.queryItems[1].value = $route.params.parentId || '0'
      if (this.dataForm.parentId !== '0') {
        this.mixinViewModuleOptions.getDataListURL = '/arbitrationFlow/page'
        this.mixinViewModuleOptions.getDataListIsPage = false
        // this.queryItems[0].value = $route.params.name || ''
        this.dataForm.name = $route.params.name || ''
      }
      this.dataList = []
      this.getDataList()
    }
  },
  methods: {
    ...mapMutations('d2admin/page', {
      d2adminPagePoolPush: 'push'
    }),
    // 子级
    childHandle (row) {
      // 组装路由名称, 并判断是否已添加, 如是: 则直接跳转
      var routeName = `${this.$route.name}__${row.id}`
      var route = window.SITE_CONFIG['dynamicRoutes'].filter(item => item.name === routeName)[0]
      if (route) {
        return this.$router.push({ name: routeName, params: { 'parentId': row.id } })
      }
      // 否则: 添加并全局变量保存, 再跳转
      route = {
        path: routeName,
        component: () => import(`@/views/zfw/${this.$route.name.replace(/-/g, '/')}`),
        name: routeName,
        meta: {
          ...window.SITE_CONFIG['contentTabDefault'],
          menuId: this.$route.meta.menuId,
          title: `${this.$route.meta.title} - ${row.name}`
        }
      }
      this.$router.addRoutes([
        {
          ...moduleRoutes,
          name: `main-dynamic__${route.name}`,
          children: [route]
        }
      ])
      window.SITE_CONFIG['dynamicRoutes'].push(route)
      this.d2adminPagePoolPush(route)
      this.$router.push({ name: route.name, params: { 'parentId': row.id, 'name': row.name } })
    },
    // 新增 / 修改
    addOrUpdateHandle (row = {}) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.dataForm.id = row.id
        this.$refs.addOrUpdate.dataForm.parentId = this.dataForm.parentId
        this.$refs.addOrUpdate.dataForm.type = row.type || this.dataForm.type || ''
        this.$refs.addOrUpdate.init()
      })
    }
  }
}
</script>
